<?php
namespace App\Controller;

use Think\Cache;

class CartController extends ApiController
{
      //购物车信息显示
      public function cart(){
          if(IS_POST) {
              $user_id=I('post.user_id');
              if (!is_numeric($user_id)) {
                  json_Response('-200','请先登录','null',2);
              }
              $count=D('User')->where("id={$user_id}")->count();
              if(!$count){
                  json_Response('-200','请先登录','null',2);
              }

              $data = D('Cart')->where("user_id = {$user_id}")->select();
              foreach ($data as $k => &$v) {
                  if($v['supplier_id']==0){
                      $model = M('Seller_selfgoods');
                      $goods = $model->field("goods_number,goods_name,goods_price")->find($v['goods_id']);
                      $v['goods_name'] = $goods['goods_name'];
                      $v['goods_price'] = $goods['goods_price'];
                      $v['goods_number'] = $goods['goods_number'];
                      $price = D('Goodspic')->where("goods_id = {$v['goods_id']} and status=1")->getField('picture_mid');
                      $v['picture_mid'] = C('api_address').$price;
                      $attr = M('GoodsAttr')->alias('a1')
                          ->field("attr_name,attr_value")
                          ->join('left join tpshop_attribute a2 on a1.attr_id = a2.attr_id')
                          ->where("a1.id in ({$v['goods_attr_ids']})")
                          ->select();
                      $v['attr'] = $attr;
                  }else{
                      $model = M('Goods');
                      $goods = $model->field("goods_number,goods_name,goods_price")->find($v['goods_id']);
                      $v['goods_name'] = $goods['goods_name'];
                      $v['goods_price'] = $goods['goods_price'];
                      $v['goods_number'] = $goods['goods_number'];
                      $price = D('Goodspic')->where("goods_id = {$v['goods_id']} and status=0")->getField('picture_mid');
                      $v['picture_mid'] = C('api_address').$price;
                      $attr = M('GoodsAttr')->alias('a1')
                          ->field("attr_name,attr_value")
                          ->join('left join tpshop_attribute a2 on a1.attr_id = a2.attr_id')
                          ->where("a1.id in ({$v['goods_attr_ids']})")
                          ->select();
                      $v['attr'] = $attr;
                  }
              }
//              dump($data);die;
              if (empty($data)) {
                  json_Response(-200, '传输失败', $data=[], 1);
              } else {
                  json_Response(200, '传输成功', $data, 1);
              }
      }else{
              json_Response('-200','传输失败','null',2);
          }
      }
      //购物车删除
      public function cart_del(){
          if(IS_POST) {
              $user_id=I('post.user_id');
              if (!is_numeric($user_id)) {
                  json_Response('-200','请先登录','null',2);
              }
              $count=D('User')->where("id={$user_id}")->count();
              if(!$count){
                  json_Response('-200','请先登录','null',2);
              }
              $cart_id=I('post.cart_id');
              if (!is_numeric($cart_id)) {
                  json_Response('-200','参数错误','null',2);
              }
              $res = D('Cart')->delete($cart_id);
              if ($res){
                  json_Response('200','删除成功',$res,2);
              }else{
                  json_Response('200','删除失败',$res,2);
              }

          }else{
              json_Response('-200','传输失败','null',2);
          }
      }
      //购物车批量删除
      public function cart_batch_del(){
          if(IS_POST) {
              $user_id=I('post.user_id');
              if (!is_numeric($user_id)) {
                  json_Response('-200','请先登录','null',2);
              }
              $count=D('User')->where("id={$user_id}")->count();
              if(!$count){
                  json_Response('-200','请先登录','null',2);
              }
              $cart_ids=I('post.cart_ids');
              if(empty($cart_ids)){
                  json_Response('-200','参数错误','null',2);
              }
              $cart_ids=str_replace("，",",",$cart_ids);
              $res = D('Cart')->delete($cart_ids);
              if ($res){
                  json_Response('200','删除成功',$res,2);
              }else{
                  json_Response('200','删除失败',$res,2);
              }

          }else{
              json_Response('-200','传输失败','null',2);
          }
      }
      //购物车修改数量
      public function cart_mod_num()
     {
         if(IS_POST) {
             $user_id=I('post.user_id');
             if (!is_numeric($user_id)) {
                 json_Response('-200','请先登录','null',2);
             }
             $count=D('User')->where("id={$user_id}")->count();
             if(!$count){
                 json_Response('-200','请先登录','null',2);
             }
             $cart_id=I('post.cart_id');
             if (!is_numeric($cart_id)) {
                 json_Response('-200','参数错误','null',2);
             }
             $number=I('post.number');
             if (!is_numeric($number)) {
                 json_Response('-200','参数错误','null',2);
             }
             $data['id']=$cart_id;
             $data['number']=$number;
             $res = D('Cart')->save($data);
             if ($res){
                 json_Response('200','修改成功',$res,2);
             }else{
                 json_Response('200','修改失败',$res,2);
             }

         }else{
             json_Response('-200','传输失败','null',2);
         }
     }


}
